package org.listbeast.gui;

/**
 * 
 */
import java.lang.reflect.Method;
import java.net.ProxySelector;
import java.net.URL;
import java.net.URLClassLoader;

import org.listbeast.core.common.ApplicationException;
import org.listbeast.core.common.ApplicationInformationImpl;

import com.btr.proxy.search.ProxySearch;
import com.btr.proxy.search.ProxySearch.Strategy;
import com.btr.proxy.util.PlatformUtil;
import com.btr.proxy.util.PlatformUtil.Platform;

public class ListBeast {

	public static void main(String... args) {
		
		ListBeast application = new ListBeast();
		
		//Load machine-dependent swt jar file
		application.loadSwtJar();
		
		//Auto detect the platform network proxy settings - uses proxy-vole
		application.loadProxySettings();
		
		//execute some basic initializations
		ApplicationException.initialize(new ApplicationInformationImpl());
		
		//Launch the application
		try {
			ListBeastSWT window = new ListBeastSWT();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void loadSwtJar() {
		String swtFileName = "";
		
	    try {
	        String osName = System.getProperty("os.name").toLowerCase();
	        String osArch = System.getProperty("os.arch").toLowerCase();
	        URLClassLoader classLoader = (URLClassLoader) getClass().getClassLoader();
	        Method addUrlMethod = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
	        addUrlMethod.setAccessible(true);

	        String swtFileNameOsPart = 
	            osName.contains("win") ? "win32-win32" :
	            osName.contains("mac") ? "cocoa-macosx" :
	            osName.contains("linux") || osName.contains("nix") ? "gtk-linux" :
	            ""; // throw new RuntimeException("Unknown OS name: "+osName)

	        String swtFileNameArchPart = osArch.contains("64") ? "x86_64" : "x86";
	        swtFileName = "lib/swt-3.7.2-"+swtFileNameOsPart+"-"+swtFileNameArchPart+"-3.7.2.jar";
	        URL swtFileUrl = new URL("file:"+swtFileName); // I am using Jar-in-Jar class loader which understands this URL; adjust accordingly if you don't
	        addUrlMethod.invoke(classLoader, swtFileUrl);
	    }
	    catch(Exception e) {
	        System.out.println("Unable to add the swt jar to the class path: "+swtFileName);
	        e.printStackTrace();
	    }
	}
	
	private void loadProxySettings() {
		ProxySearch proxySearch = new ProxySearch();
        
    	if (PlatformUtil.getCurrentPlattform() == Platform.WIN) {
    	  proxySearch.addStrategy(Strategy.IE);
    	  proxySearch.addStrategy(Strategy.FIREFOX);
    	  proxySearch.addStrategy(Strategy.JAVA);
    	} else 
    	if (PlatformUtil.getCurrentPlattform() == Platform.LINUX) {
    	  proxySearch.addStrategy(Strategy.GNOME);
    	  proxySearch.addStrategy(Strategy.KDE);
    	  proxySearch.addStrategy(Strategy.FIREFOX);
    	} else {
    	  proxySearch.addStrategy(Strategy.OS_DEFAULT);
    	}

    	ProxySelector myProxySelector = proxySearch.getProxySelector();
    	                
    	ProxySelector.setDefault(myProxySelector);
	}
	
}
